// reservation.js - 预约相关API
import request from '../plugins/axios';

// 模拟预约记录数据
const mockReservations = [
  {
    id: 1,
    user_id: 2, // user表中的普通用户ID
    vehicle_id: 1, // vehicle表中的车辆ID
    parking_space_id: 5, // parking_space表中的车位ID
    parking_lot_id: 1, // 关联的停车场ID
    space_number: 'A105', // 车位编号
    plate_number: '粤B12345', // 车牌号码
    start_time: '2025-03-08 09:00:00',
    end_time: '2025-03-08 18:00:00',
    status: 1, // 0-待审核，1-已确认，2-已使用，3-已取消，4-已拒绝
    fee: 25.00,
    is_paid: 1, // 0-未支付，1-已支付
    payment_id: 1,
    remark: '工作日一天预约',
    created_at: '2025-03-06 10:30:00',
    updated_at: '2025-03-06 15:45:00'
  },
  {
    id: 2,
    user_id: 2,
    vehicle_id: 2,
    parking_space_id: 8,
    parking_lot_id: 1,
    space_number: 'A108',
    plate_number: '粤B67890',
    start_time: '2025-03-09 08:00:00',
    end_time: '2025-03-09 17:00:00',
    status: 0, // 待审核
    fee: 25.00,
    is_paid: 0, // 未支付
    payment_id: null,
    remark: '周末一天预约',
    created_at: '2025-03-08 09:15:00',
    updated_at: '2025-03-08 09:15:00'
  },
  {
    id: 3,
    user_id: 2,
    vehicle_id: 1,
    parking_space_id: 12,
    parking_lot_id: 1,
    space_number: 'A112',
    plate_number: '粤B12345',
    start_time: '2025-03-07 14:00:00',
    end_time: '2025-03-07 18:00:00',
    status: 2, // 已使用
    fee: 15.00,
    is_paid: 1,
    payment_id: 2,
    remark: '下午半天预约',
    created_at: '2025-03-06 11:20:00',
    updated_at: '2025-03-07 18:30:00'
  },
  {
    id: 4,
    user_id: 2,
    vehicle_id: 1,
    parking_space_id: 20,
    parking_lot_id: 1,
    space_number: 'B105',
    plate_number: '粤B12345',
    start_time: '2025-03-06 09:00:00',
    end_time: '2025-03-06 12:00:00',
    status: 3, // 已取消
    fee: 10.00,
    is_paid: 0,
    payment_id: null,
    remark: '临时取消预约',
    created_at: '2025-03-05 16:45:00',
    updated_at: '2025-03-05 20:10:00'
  },
  {
    id: 5,
    user_id: 2,
    vehicle_id: 2,
    parking_space_id: 25,
    parking_lot_id: 1,
    space_number: 'B110',
    plate_number: '粤B67890',
    start_time: '2025-03-10 09:00:00',
    end_time: '2025-03-14 18:00:00',
    status: 1, // 已确认
    fee: 100.00,
    is_paid: 1,
    payment_id: 3,
    remark: '工作周长期预约',
    created_at: '2025-03-07 14:20:00',
    updated_at: '2025-03-07 16:15:00'
  },
  {
    id: 6,
    user_id: 2,
    vehicle_id: 1,
    parking_space_id: 30,
    parking_lot_id: 2,
    space_number: 'C105',
    plate_number: '粤B12345',
    start_time: '2025-03-15 08:00:00',
    end_time: '2025-03-15 20:00:00',
    status: 4, // 已拒绝
    fee: 30.00,
    is_paid: 0,
    payment_id: null,
    remark: '车位已被预约',
    created_at: '2025-03-08 08:30:00',
    updated_at: '2025-03-08 10:45:00'
  }
];

// 根据预约状态获取名称
export function getStatusText(status) {
  const statusMap = {
    0: '待审核',
    1: '已确认',
    2: '已使用',
    3: '已取消',
    4: '已拒绝'
  };
  return statusMap[status] || '未知状态';
}

// 获取用户预约列表 - 使用真实API
export function getUserReservations(params = {}) {
  return request({
    url: '/api/reservations',
    method: 'get',
    params
  });
}

// 创建预约 - 使用真实API
export function createReservation(data) {
  return request({
    url: '/api/reservations',
    method: 'post',
    data
  });
}

// 取消预约 - 使用真实API
export function cancelReservation(id) {
  return request({
    url: `/api/reservations/${id}/cancel`,
    method: 'put'
  });
}

// 获取预约详情 - 使用真实API
export function getReservationDetail(id) {
  return request({
    url: `/api/reservations/${id}`,
    method: 'get'
  });
}

// 确认预约 - 使用真实API
export function confirmReservation(id) {
  return request({
    url: `/api/reservations/${id}/confirm`,
    method: 'put'
  });
}

// 拒绝预约 - 使用真实API
export function rejectReservation(id, remark) {
  return request({
    url: `/api/reservations/${id}/reject`,
    method: 'put',
    params: { remark }
  });
}

// 获取可用时间段
export function getAvailableTimeSlots(date, parkingSpaceId) {
  return new Promise((resolve) => {
    setTimeout(() => {
      // 预定义的时间段
      const allTimeSlots = [
        { start: '08:00', end: '10:00' },
        { start: '10:00', end: '12:00' },
        { start: '12:00', end: '14:00' },
        { start: '14:00', end: '16:00' },
        { start: '16:00', end: '18:00' },
        { start: '18:00', end: '20:00' }
      ];
      
      // 模拟当前日期的某些时间段已被预约
      const unavailableSlots = date === '2025-03-08' ? [0, 2, 4] : date === '2025-03-09' ? [1, 3] : [];
      
      // 过滤出可用的时间段
      const availableSlots = allTimeSlots.filter((_, index) => !unavailableSlots.includes(index));
      
      resolve({
        code: 200,
        message: '获取成功',
        data: availableSlots
      });
    }, 300);
  });
}

// 获取预约统计信息 - 使用真实API
export function getReservationStats() {
  return request({
    url: '/api/reservations/statistics',
    method: 'get'
  });
}

// 获取支付方式列表 - 使用真实API
export function getPaymentMethods() {
  return request({
    url: '/api/reservations/payment-methods',
    method: 'get'
  });
}

// 获取预约列表（管理员）
export function getReservationList(params) {
  return request({
    url: '/api/reservations',
    method: 'get',
    params
  })
}

// 审核预约
export function reviewReservation(id, data) {
  const url = data.status === 1 ? 
    `/api/reservations/${id}/confirm` : 
    `/api/reservations/${id}/reject`;
  
  return request({
    url,
    method: 'put',
    params: data.status === 4 ? { remark: data.remark } : undefined
  });
}

// 批量审核预约
export function batchReviewReservations(data) {
  return Promise.all(
    data.ids.map(id => reviewReservation(id, {
      status: data.status,
      remark: data.remark
    }))
  );
} 